package zzs.generalframework.console.core.parameter.commonparameter.nationality.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.commonparameter.nationality.NationalityInfo;
import zzs.generalframework.common.core.parameter.commonparameter.nationality.NationalityPO;
import zzs.generalframework.console.core.parameter.commonparameter.nationality.dao.INationalityDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.nationality.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.commonparameter.nationality.dao.impl.nationalityDaoImpl")
public class NationalityDaoImpl implements INationalityDao
{
	private Logger logger = LoggerFactory.getLogger(NationalityDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(NationalityPO nationalityPO)
	{
		logger.info("====插入民族数据开始====");
		logger.debug("====插入民族数据,入参:NationalityPO=[{}]====", nationalityPO);
		entityManager.persist(nationalityPO);
		logger.debug("====插入民族数据,出参:boolean=[{}]====", true);
		logger.info("====插入民族数据结束====");
		return true;
	}

	@Override
	public boolean update(NationalityPO nationalityPO)
	{
		logger.info("====更新民族数据开始====");
		logger.debug("====更新民族数据,入参:NationalityPO=[{}]====", nationalityPO);
		entityManager.merge(nationalityPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新民族数据结束====");
		return true;
	}

	@Override
	public List<NationalityPO> select(NationalityInfo nationalityInfo)
	{
		logger.info("====查询民族数据开始====");
		logger.debug("====查询民族数据,入参:NationalityInfo=[{}]====", nationalityInfo);
		List<NationalityPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from NationalityPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(nationalityInfo.getNationalityID()))
		{
			sqlBuffer.append(" and a.nationalityID=?");
			parameter.putParameterObject(nationalityInfo.getNationalityID());
		}
		if (StringUtils.isNotNullAndBlank(nationalityInfo.getNationalityCode()))
		{
			sqlBuffer.append(" and a.nationalityCode=?");
			parameter.putParameterObject(nationalityInfo.getNationalityCode());
		}
		if (StringUtils.isNotNullAndBlank(nationalityInfo.getNationalityName()))
		{
			sqlBuffer.append(" and a.nationalityName=?");
			parameter.putParameterObject(nationalityInfo.getNationalityName());
		}
		if (StringUtils.isNotNullAndBlank(nationalityInfo.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(nationalityInfo.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(nationalityInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(nationalityInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询民族数据,出参:List<NationalityPO>=[{}]====", returnList);
		logger.info("====查询民族数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(NationalityPO nationalityPO)
	{
		logger.info("====删除民族数据开始====");
		logger.debug("====删除民族数据,入参:NationalityPO=[{}]====", nationalityPO);
		entityManager.remove(nationalityPO);
		logger.debug("====删除民族数据,出参:boolean=[{}]====", true);
		logger.info("====删除民族数据结束====");
		return true;
	}

	@Override
	public Long count(NationalityPO nationalityPO)
	{
		logger.info("====统计民族数据开始====");
		logger.debug("====统计民族数据,入参:NationalityPO=[{}]====", nationalityPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from NationalityPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(nationalityPO.getNationalityID()))
		{
			sqlBuffer.append(" and a.nationalityID=?");
			parameter.putParameterObject(nationalityPO.getNationalityID());
		}
		if (StringUtils.isNotNullAndBlank(nationalityPO.getNationalityCode()))
		{
			sqlBuffer.append(" and a.nationalityCode=?");
			parameter.putParameterObject(nationalityPO.getNationalityCode());
		}
		if (StringUtils.isNotNullAndBlank(nationalityPO.getNationalityName()))
		{
			sqlBuffer.append(" and a.nationalityName=?");
			parameter.putParameterObject(nationalityPO.getNationalityName());
		}
		if (StringUtils.isNotNullAndBlank(nationalityPO.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(nationalityPO.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(nationalityPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(nationalityPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计民族数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计民族数据结束====");
		return returnLong;
	}

}
